using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Amplify.Twitter;
using LotroTweeter.Properties;

namespace LotroTweeter.Engine
{
    public class TwitterPublisher
    {
        private ConsoleText consoleText;
        private LotroWatcher watcher;

        public TwitterPublisher(ConsoleText consoleText, LotroWatcher lotroWatcher)
        {
            watcher = lotroWatcher;
            watcher.LotroWatcherInitializing += Init;
            watcher.LevelUpEvent += LevelUpEvent;
            watcher.NewMemberEvent += NewMemberEvent;
            watcher.LotroWatcherStopping += Close;
            watcher.ErrorOccurred += (sender, e) => consoleText.WriteLine(e.Exception.Message);
            this.consoleText = consoleText;
        }

        private void NewMemberEvent(object sender, IEnumerable<LotroEventArgs> lotroWatcherEventArgs)
        {
            consoleText.WriteLine("New Members!");
            foreach (var arg in lotroWatcherEventArgs)
            {
                var m = arg.Member;
                consoleText.WriteLine(string.Format("{0} is a new member", m.Name));
                var tweet = string.Format("We have a new member! {0} is a {1} {2}. Welcome to the kin!", m.Name, m.Race, m.Class);
                client.Create(tweet);
            }
        }

        private void LevelUpEvent(object sender, IEnumerable<LotroEventArgs> lotroWatcherEventArgs)
        {
            consoleText.WriteLine("Level ups!");
            foreach (var arg in lotroWatcherEventArgs)
            {
                var m = arg.Member;
                consoleText.WriteLine(string.Format("{0} has hit {1}",m.Name,m.Level));
                string tweet = "";
                List<int> standardAnnouncementLevels = new List<int>(){10,15,20,25,30,32,34,35,36,38,40,42,44,45,46,48,50,51,52,53,54,55,56,57,58};
                var interestingLevelUp = standardAnnouncementLevels.Any(il => il == m.Level);

                if (interestingLevelUp)
                {
                    tweet = string.Format("Ding! {0} has just hit level {1}. Gratz!", m.Name, m.Level);
                }
                if (m.Level == 59)
                {
                    tweet = string.Format("Ding! {0} has just hit level {1}. Only one more to go!", m.Name, m.Level);
                }
                if (m.Level == 60)
                {
                    tweet = string.Format("DING! DING! DING! DING! DING! {0} has just hit 60! Awesome stuff! Well done :-)", m.Name);
                }
                if (tweet.Length > 0)
                {
                    client.Create(tweet);
                }
            }
        }

        private StatusClient client;

        public void Init(object sender, EventArgs e)
        {
            Twitter.SetCredentials(Settings.Default.TwitterAccount, Settings.Default.TwitterPassword);
            client = Twitter.CreateStatusClient();
            //client.Create("It's OK, I'm back on again.");
        }

        public void Close(object sender, EventArgs e)
        {
            //client.Create("I'm being turned off :-(");
        }
    }
}